package com.cmy;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLDemo {
	Document document;//文档对象
	public static void main(String[] args) throws Exception {
		XMLDemo demo = new XMLDemo();
		demo.getDOCUMENT();
//		demo.addElement();
		//调用保存文件的方法
//		demo.deleteElement();
//		demo.modityElement();
		demo.saveXML();
		demo.getPhoneById("001");



	}

	//获取文档对象
	public void getDOCUMENT() throws Exception {
		//创建SAX解析器
		SAXReader reader = new SAXReader();
		document = reader.read(this.getClass().getResourceAsStream("/NewFile.xml"));
	}

	//增加元素操作
	public void addElement() {
		//dom4j的特点:通过根节点去操作其他的节点

		Element root = document.getRootElement();//获取文档中的根节点
		//增加手机节点-------------------------------
		//返回值为刚刚增加的节点对象(phone);
		Element phone = root.addElement("phone");
		//为phone绑定id属性
		phone.addAttribute("id", "003");
		//依次增加子节点
		//获取品牌节点(bread);
		Element brand = phone.addElement("brand");
		//获取型号节点(type);
		Element type = phone.addElement("type");
		//获取价格节点(price);
		Element price = phone.addElement("price");
		//获取描述节点(description);
		Element description = phone.addElement("description");

		//设置文本值
		brand.addText("小米");
		type.addText("Max3");
		price.addText("3600");
		description.addText("买一送一");




	}



	//删除元素操作
	public void deleteElement() {
		Element root = document.getRootElement();
		//属性的迭代器
		Iterator<Element> iter = root.elementIterator();

		while(iter.hasNext()) {
			Element ele = iter.next();
			if(ele.attributeValue("id").equals("002")) {
				root.remove(ele);
				break;
			}
		}




	}

	//修改元素操作
	public void modityElement() {
		Element root = document.getRootElement();

		//	Element elementByID = root.elementByID("003");//通过id属性直接获取节点对象

		Iterator<Element> iter1 = root.elementIterator();
		while(iter1.hasNext()) {
			Element element = iter1.next();
			String id = element.attributeValue("id");
			if(id.equals("003")) {
				Iterator<Element> iterator = element.elementIterator();

				while(iterator.hasNext()) {
					Element ele = iterator.next();
					if(ele.getName().equals("price")) {
						ele.setText("3000");

					}
				}
				break;
			}
		}
	}

	//查询元素操作
	public void getPhoneById(String id) {
		Element root = document.getRootElement();

		Iterator<Element> iterator = root.elementIterator();

		while(iterator.hasNext()) {
			Element next = iterator.next();
			if(next.attributeValue("id").equals(id)) {
				//phone节点下的子节点
				Iterator<Element> subs = next.elementIterator();
				while(subs.hasNext()) {
					System.out.println(subs.next().getText()+" ");
				}
				break;

			}
		}

	}










	//保存操作

	public void saveXML() throws Exception {
		FileWriter fw = new FileWriter(new File("D:\\eclipse\\XML&JSON\\resources\\NewFile.xml"));


		OutputFormat format = OutputFormat.createPrettyPrint();//创建输出格式

		//设置字符集
		format.setEncoding("GBK");

		XMLWriter writer = new XMLWriter(fw,format);

		writer.write(document);

		writer.flush();

		writer.close();

		fw.close();
	}



}
